package com.bowen.demo.demo004;

import java.util.Random;

/**
 * <h3>java-study</h3>
 * <p>获取随机数工具类</p>
 * @author : zhang.bw
 * @date : 2021-03-10 13:53
 **/
public final class RandomUtils {

	/**
	 * 用于随机选的字符和数字
	 */
	public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/**
	 * 返回一个定长的随机字符串(只包含大小写字母、数字)
	 *
	 * @param length 随机字符串长度
	 * @return 随机字符串
	 */
	public static String getString(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
		}
		return sb.toString();
	}
}
